package com.ss.android.article.base.feature.app.d;

import com.bytedance.common.utility.Logger;
import com.ss.android.action.b.ImpressionHelper;
import com.ss.android.action.b.b;
import com.ss.android.action.b.FeedViewHolder;
import com.ss.android.common.app.LifeCycleMonitor;

import java.util.ArrayList;
import java.util.List;

public class CommonImpressionAdapter implements com.ss.android.action.b.a, LifeCycleMonitor {
    private String a;
    private final b b;
    private final List c;
    private int d;
    private int e;
    private boolean f;
    private int[] g;
    private boolean h;

    private CommonImpressionAdapter(String arg5, int arg6) {
        int v3 = -1;

        this.d = v3;
        this.e = v3;
        if (Logger.debug()) {
            Logger.d("CommonImpressionAdapter", "CommonImpressionAdapter : " + arg5);
        }

        this.a = arg5;
        this.c = new ArrayList();
        this.b = ImpressionHelper.getInstance().a(arg6, arg5, null);
        this.f = false;
        this.d = v3;
        this.e = v3;
        this.h = true;
        this.g = new int[4];
    }

    public CommonImpressionAdapter(String arg1, int arg2, byte arg3) {
        this(arg1, arg2);
    }

    public String a(int arg2) {
        return String.valueOf(arg2);
    }

    public final void a(int arg6, int arg7) {
        if (arg6 > 0) {
            int v0 = this.c.size();
            if (v0 >= arg6) {
                return;
            }

            while (v0 < arg6) {
                FeedViewHolder v1 = new FeedViewHolder();
                String v2 = this.a(v0);
                v1.a(arg7, v2);
                this.c.add(v1);
                if (Logger.debug()) {
                    Logger.d("CommonImpressionAdapter", "build: position = " + v0 + ", id = " + v2);
                }

                ++v0;
            }
        }
    }

    public final boolean a() {
        return this.h;
    }

    public final boolean a(int arg2, FeedViewHolder arg3) {
        boolean v0 = this.d > arg2 || arg2 > this.e ? false : true;
        return v0;
    }

    public final List b() {
        return this.c;
    }

    public final void b(int arg9, int arg10) {
        int v7 = 3;
        int v6 = 2;
        int v3 = -1;
        if (arg9 <= arg10 && (this.h) && (arg9 != this.d || arg10 != this.e)) {
            if (Logger.debug()) {
                Logger.d("CommonImpressionAdapter", "onScrollChange : firstPosition = " + arg9 + ", lastPosition = " + arg10);
            }

            this.g[0] = this.d;
            this.g[1] = this.e;
            this.g[v6] = arg9;
            this.g[v7] = arg10;
            this.g();
            this.h();
            this.g[0] = v3;
            this.g[1] = v3;
            this.g[v6] = v3;
            this.g[v7] = v3;
            this.d = arg9;
            this.e = arg10;
        }
    }

    public final void onDestroy() {
        if (Logger.debug()) {
            Logger.d("CommonImpressionAdapter", "onDestroy :" + this.a);
        }

        this.f();
        this.h = false;
    }

    public final void onResume() {
        if (Logger.debug()) {
            Logger.d("CommonImpressionAdapter", "onResume :" + this.a);
        }

        this.h = true;
        if (this.b != null) {
            this.b.a(((com.ss.android.action.b.a) this));
            this.f = false;
        }
    }

    public final void f() {
        if (this.h) {
            this.onPause();
        }

        if (Logger.debug()) {
            Logger.d("CommonImpressionAdapter", "packAndClearImpression :" + this.a);
        }

        ImpressionHelper.getInstance().a(this.b, this.a);
    }

    public final void onPause() {
        if (Logger.debug()) {
            Logger.d("CommonImpressionAdapter", "onPause :" + this.a);
        }

        if (!this.f && this.b != null) {
            this.b.b(((com.ss.android.action.b.a) this));
            this.f = true;
        }

        this.h = false;
    }

    private void g() {
        int v3 = -1;
        if (this.h) {
            int v0 = this.g[2];
            int v2 = this.g[3];
            if (v0 == v3 && v2 == v3) {
                return;
            }

            int v1;
            for (v1 = v0; v1 <= v2; ++v1) {
                if (v1 >= 0) {
                    if (v1 < this.c.size()) {
                        Object v0_1 = this.c.get(v1);
                        if (v0_1 != null && !((FeedViewHolder) v0_1).n_()) {
                            this.b.a(((FeedViewHolder) v0_1));
                            if (Logger.debug()) {
                                Logger.d("CommonImpressionAdapter", "getView : " + v1);
                            }
                        }
                    } else {
                        return;
                    }
                }
            }
        }
    }

    private void h() {
        if (this.h) {
            int v0 = this.g[0];
            int v2 = this.g[1];
            int v3 = this.g[2];
            int v4 = this.g[3];
            int v1;
            for (v1 = v0; v1 <= v2; ++v1) {
                if (v1 >= 0) {
                    if (v1 < this.c.size()) {
                        if (v1 >= v3 && v1 <= v4) {
                            continue;
                        }

                        Object v0_1 = this.c.get(v1);
                        if (v0_1 == null) {
                            continue;
                        }

                        if (!((FeedViewHolder) v0_1).n_()) {
                            continue;
                        }

                        this.b.b(((FeedViewHolder) v0_1));
                        if (!Logger.debug()) {
                            continue;
                        }
                        Logger.d("CommonImpressionAdapter", "onMovedToScrapHeap : " + v1);
                    } else {
                        return;
                    }
                }
            }
        }
    }

    public final void onStop() {
        if (Logger.debug()) {
            Logger.d("CommonImpressionAdapter", "onStop :" + this.a);
        }

        this.h = false;
    }
}

